package com.atguigu.order.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author polaris
 * @date 2021/1/26 7:59 下午
 */
@Configuration
public class MyFeignConfig {
  @Bean("requestInterceptor")
  public RequestInterceptor requestInterceptor() {
    return new RequestInterceptor() {
      @Override
      public void apply(RequestTemplate requestTemplate) {
        //1、RequestContextHandler拿到刚进来的请求
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();//老请求
        String cookie = request.getHeader("Cookie");
        //给新请求同步老请求cookie
        requestTemplate.header("Cookie", cookie);
      }
    };
  }
}
